package com.example.jetpacktest.livedata

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.SeekBar
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider

import com.example.jetpacktest.R
import kotlinx.android.synthetic.main.fragment_fragment3.view.*

class Fragment3 : Fragment() {
    var viewModel: ProgressWithLiveDataViewModel? = null
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        var view = inflater.inflate(R.layout.fragment_fragment3, container, false)
        viewModel = ViewModelProvider(activity!!).get(ProgressWithLiveDataViewModel::class.java)
        viewModel?.progress?.observe(activity!!, object : Observer<Int> {
            override fun onChanged(t: Int?) {
                activity?.runOnUiThread {
                    view?.progress?.progress = t!!
                }
            }
        })
        view?.progress?.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
                viewModel?.progress?.value = progress
            }

            override fun onStartTrackingTouch(seekBar: SeekBar?) {
            }

            override fun onStopTrackingTouch(seekBar: SeekBar?) {
            }
        })
        return view
    }

}
